package com.cdut.logistics.service.impl.businesses;

import java.util.ArrayList;
import java.util.List;

import com.aliyuncs.exceptions.ClientException;
import com.cdut.logistics.action.message.MessageUtils;
import com.cdut.logistics.config.Constants;
import com.cdut.logistics.config.service;
import com.cdut.logistics.model.dao.businesses.RepairDao;
import com.cdut.logistics.model.dao.businesses.UserDao;
import com.cdut.logistics.model.dao.datacentre.Area;
import com.cdut.logistics.model.dao.datacentre.Dict;
import com.cdut.logistics.model.dao.datacentre.School;
import com.cdut.logistics.model.dao.datacentre.User;
import com.cdut.logistics.model.entity.Model.Dorm;
import com.cdut.logistics.model.entity.Model.Repair_Detail;
import com.cdut.logistics.model.entity.Model.Repair_Order;
import com.cdut.logistics.service.Dto.RepairDto;
import com.cdut.logistics.service.interf.businesses.RepairService;
import com.jfinal.plugin.activerecord.Record;

@service("repairService")
public class RepairServiceImp implements RepairService{

	public boolean repair_save(int user_id,int school_id, int dorm_id, int area_id,int repair_type, String repair_text, String img_url,String video_url, String remarks) {
		//判断是否近期任务有他
		boolean is_task    = RepairDao.dao.is_task(user_id);
		boolean is_success = RepairDao.dao.repair_save(user_id, school_id, dorm_id, area_id, repair_type, repair_text, img_url, video_url, remarks,is_task);
		return is_success;
	}

	@Override
	public RepairDto repair_detail(int user_id) {
		RepairDto repairDto =new RepairDto();
		User    user = RepairDao.dao.user_detail(user_id);
		 if(user !=null)
		 {
		   School  school = RepairDao.dao.school_detail(user.getSchool_id());
		   if(school!=null)
		   {
			   Dorm  dorm = RepairDao.dao.dorm_detail(user.getDorm_id());
			   List<Area> area = RepairDao.dao.area_detail(school.getInt("id"));
			   List<Dict> sys_dict = RepairDao.dao.sys_dict_detail(1);
			   
			   //
			   
			   Area area_single = Area.dao.findById(dorm.getInt("area_id")); 
			   //
			   repairDto.setArea_id(area_single.getId());
			   repairDto.setArea_name(area_single.getStr("name"));
			   repairDto.setDorm_id(dorm.getInt("id"));
			   repairDto.setDorm_name(dorm.getStr("name"));
			   repairDto.setSchool_id(school.getInt("id"));
			   repairDto.setSchool_name(school.getStr("name"));
			   repairDto.setArea_list(area);
			   repairDto.setDict_list(sys_dict);
			   
			   return  repairDto;
		   }
		 }
		
		return null;
	}
	
	public List<RepairDto> repairlist_detail(int user_id)
	{
		List<RepairDto> list = new ArrayList<RepairDto>();
		List<Repair_Order>  list_record = RepairDao.dao.list_order(user_id);
		for(Repair_Order record:list_record )
		{
			RepairDto repairDto= Repairsingle_detail(record.getInt("id"),user_id);
			list.add(repairDto);
		}
		return list;
	}
	/**
	 * 查询未通过审核的
	 */
	 public List<RepairDto> admin_repairlist_detail_baoxiu()
	 {
		 List<RepairDto> list = new ArrayList<RepairDto>();
		 List<Repair_Order>  list_record = RepairDao.dao.list_order_baoxiu();
		 for(Repair_Order record:list_record )
		 {
				RepairDto repairDto= admin_Repairsingle_detail(record.getInt("id"));
				list.add(repairDto);
		 }
		return list;
		 
	 }
	/**
	 * 查询未安排的
	 */
	 public List<RepairDto> admin_repairlist_detail()
	 {
		 List<RepairDto> list = new ArrayList<RepairDto>();
		 List<Repair_Order>  list_record = RepairDao.dao.list_order_anpai();
		 for(Repair_Order record:list_record )
		 {
				RepairDto repairDto= admin_Repairsingle_detail(record.getInt("id"));
				list.add(repairDto);
		 }
		return list;
		 
	 }
	    /**
		 * 查询未调度的
		 */
		 public List<RepairDto> admin_repairlist_detail_diaodu()
		 {
			 List<RepairDto> list = new ArrayList<RepairDto>();
			 List<Repair_Order>  list_record = RepairDao.dao.list_order_diaodu();
			 for(Repair_Order record:list_record )
			 {
					RepairDto repairDto= admin_Repairsingle_detail(record.getInt("id"));
					list.add(repairDto);
			 }
			return list;
			 
		 }
		 /*
		  * 查询已调度的
		  */
		 public List<RepairDto> admin_repairlist_detail_result()
		 {
			 List<RepairDto> list = new ArrayList<RepairDto>();
			 List<Repair_Order>  list_record = RepairDao.dao.list_order_result();
			 for(Repair_Order record:list_record )
			 {
					RepairDto repairDto= admin_Repairsingle_detail(record.getInt("id"));
					list.add(repairDto);
			 }
			return list;
			 
		 }
	public RepairDto Repairsingle_detail(int order_id,int user_id)
	{
		Repair_Order record = RepairDao.dao.single_order(order_id);
		RepairDto repairDto= new RepairDto();
		
		User user = UserDao.dao.query(user_id);			
		Repair_Detail repair_Detail = RepairDao.dao.list_detail(record.getInt("id"));
		
        School school = RepairDao.dao.school_detail(repair_Detail.getInt("school_id"));
        Dorm dorm     = RepairDao.dao.dorm_detail(repair_Detail.getInt("dorm_id"));
        Area area     = RepairDao.dao.area_single(repair_Detail.getInt("area_id"));
        Dict sys_dict = RepairDao.dao.sys_dict_single(repair_Detail.getInt("repair_type"));
        if(repair_Detail.getInt("repair_leader") !=null)
        {
        	User leader = UserDao.dao.query(repair_Detail.getInt("repair_leader"));	
        
        	List<User> repair_member = new ArrayList<User>();
        	String[]  repair = repair_Detail.getStr("repair_member").split("-");
        	for(String s:repair)
        	{
        		User user_repair= UserDao.dao.query(Integer.parseInt(s));	
        		repair_member.add(user_repair);
        	}
        	repairDto.setLeader(leader);
    		repairDto.setRepair_member(repair_member); 
        }
         
		repairDto.setUser(user);
		repairDto.setRepair_detail(repair_Detail);
		repairDto.setRepair_order(record);
		repairDto.setSchool_name(school.getStr("name"));
		repairDto.setArea_name(area.getStr("name"));
		if(dorm !=null)
		{
			repairDto.setDorm_name(dorm.getStr("name"));
		}
		repairDto.setDict_single(sys_dict);
		  
		
		return repairDto;
	}
	 public RepairDto admin_Repairsingle_detail(int order_id)
	 {
		 Repair_Order record = RepairDao.dao.single_order(order_id);
			RepairDto repairDto= new RepairDto();
			
			User user = UserDao.dao.query(record.getInt("user_id"));			
			Repair_Detail repair_Detail = RepairDao.dao.list_detail(record.getInt("id"));
	        School school = RepairDao.dao.school_detail(repair_Detail.getInt("school_id"));
	        Dorm dorm     = RepairDao.dao.dorm_detail(repair_Detail.getInt("dorm_id"));
	        Area area     = RepairDao.dao.area_single(repair_Detail.getInt("area_id"));
	        Dict sys_dict = RepairDao.dao.sys_dict_single(repair_Detail.getInt("repair_type"));
	      
	      if(repair_Detail.getInt("repair_leader") != null)
	      {
	    	  
	        User leader = UserDao.dao.query(repair_Detail.getInt("repair_leader"));	
	        repairDto.setLeader(leader);
	        if(repair_Detail.getStr("repair_member") !=null)
	        {
	        	 List<User> repair_member = new ArrayList<User>();
	 	        String[]  repair = repair_Detail.getStr("repair_member").split("-");
	 	        for(String s:repair)
	 	        {
	 	        	User user_repair= UserDao.dao.query(Integer.parseInt(s));	
	 	        	repair_member.add(user_repair);
	 	        }
	 	     
	 			repairDto.setRepair_member(repair_member);  
	        }
	       
	      }  
			repairDto.setUser(user);
			repairDto.setRepair_detail(repair_Detail);
			repairDto.setRepair_order(record);
			repairDto.setSchool_name(school.getStr("name"));
			repairDto.setArea_name(area.getStr("name"));
			if(dorm !=null)
			{
				repairDto.setDorm_name(dorm.getStr("name"));
			}
			repairDto.setDict_single(sys_dict);
			 
			
			return repairDto;
	 }
	public User  user_detail(int user_id)
	{
		User  user = RepairDao.dao.user_detail(user_id);
		return user;
	}
	
	 public boolean repair_task(int leader_id,int school_id, int area_id,String repair_member,String start_time,String end_time,String remarks)
	 {
		 boolean is_success =  RepairDao.dao.repair_task(leader_id, school_id, area_id, repair_member, start_time, end_time, remarks);
		 return is_success;
	 }
	 
	 public boolean repair_cancel(int order_id)
	 {
		 boolean is_success = RepairDao.dao.repair_cancel(order_id);
		 return is_success;
	 }
  
	 public boolean repair_auditing_agree(int order_id)
	 {
		 boolean is_success = RepairDao.dao.repair_auditing_agree(order_id);
		 return is_success;
	 }
	 
	 public boolean repair_auditing_disagree(int order_id,String remarks)
	 {
		 boolean is_success = RepairDao.dao.repair_auditing_disagree(order_id, remarks);
		 return is_success;
	 }
	 
	 public boolean repair_arrange(int order_id,int repair_leader)
	 {
		 Repair_Detail  repair_Detail = RepairDao.dao.list_detail(order_id);
		 Repair_Order   repair_Order  = Repair_Order.dao.findById(order_id);
		 repair_Order.set("arrangement_status", 1);
		 repair_Order.update();
		 boolean is_success = RepairDao.dao.repair_arrange(repair_leader,repair_Detail);
		 
		 //
		 User user = User.dao.findById(repair_leader);
		 user.set("user_status", 1);
		 user.update();
		 Dorm dorm = Dorm.dao.findById(repair_Detail.getInt("dorm_id"));
		 Area area = Area.dao.findById(repair_Detail.getInt("area_id"));
		 //
		 
		 String name = user.getName();//名字
		 String phone = user.getPhone();		 
		 String dorm_name =dorm.getStr("name");
		 String area_name =dorm.getStr("name");
		 //宿舍名字 电话 区域名字  维修组长的名字
		 //调接口
		 try {
			MessageUtils.notifTeamer(phone, name, area_name, dorm_name);
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 //
		 return is_success;
	 }
	 
	 public boolean repair_diaodu(int order_id,String repair_member)
	 {
		 Repair_Detail  repair_Detail = RepairDao.dao.list_detail(order_id);
		 boolean is_success = RepairDao.dao.repair_diaodu(repair_member,repair_Detail);
		 
		 Repair_Order  repair_order =Repair_Order.dao.findById(order_id);
		 repair_order.set("diaodu_status", 1);
		 repair_order.update();
		 Dorm dorm = Dorm.dao.findById(repair_Detail.getInt("dorm_id"));
		 Area area = Area.dao.findById(repair_Detail.getInt("area_id"));
		 //
		 //维修工
		 String[] repair_list =repair_member.split("-");
		 for(String s:repair_list)
		 {
			 User user = User.dao.findById(Integer.parseInt(s));
			 user.set("user_status", 1);
			 user.update();
			//宿舍名字 电话 区域名字  维修组长的名字
			 String name = user.getName();//名字
			 String phone = user.getPhone();		 
			 String dorm_name =dorm.getStr("name");
			 String area_name =area.getStr("name");
			 //调接口
			 try {
				MessageUtils.notifTeamer(phone, name, area_name, dorm_name);
			} catch (ClientException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 
		 //
		 return is_success;
	 }
	 
	 public boolean repair_result(int order_id)
	 {
		 Repair_Order repair_order   = RepairDao.dao.repair_order(order_id);
		 boolean is_success = RepairDao.dao.repair_result(repair_order);
		 
		 if(is_success)
    	 {
    		 Repair_Order repair_Order =Repair_Order.dao.findById(order_id);
    		 User user = User.dao.findById(repair_Order.getInt("user_id"));
    		 
    		 String sql = "select * from repair_detail where order_id = ? and "+Constants.sql_add;
    		 Repair_Detail repair_detail  = Repair_Detail.dao.findFirst(sql,order_id);
    		 int id = repair_detail.getInt("repair_leader");
    		 User user1 =User.dao.findById(id);
    		 user1.set("user_status", 0);
    		 user1.update();
    		 String name = user.getName();
    		 String phone = user.getPhone();
    		 //调接口
    		 try {
				MessageUtils.notifUser(phone, name);
			} catch (ClientException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		 //
 catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	 }
		 return is_success;
	 }
	 
	 public boolean repair_suggestion(int order_id,int user_id, String content)
	 {
		 boolean is_success = RepairDao.dao.repair_suggestion( order_id, user_id,  content);
		 return is_success;
	 }
	 public boolean repair_advice(int user_id, String content)
	 {
		 boolean is_success = RepairDao.dao.repair_advice( user_id,  content);
		 return is_success;
	 }


}
